package com.javatop.controller.article;

import com.javatop.pojo.entity.Draft;
import com.javatop.pojo.entity.PageBean;
import com.javatop.pojo.entity.Article;
import com.javatop.pojo.entity.Result;
import com.javatop.service.serviceInterface.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/article")
@RestController
@Api(tags = "文章管理接口")
public class ArticleController {

    private static final Logger log = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    @ApiOperation("删除文章接口")
   /* @DeleteMapping
    public Result deleteById_t(@RequestParam Integer id) {
        log.info("删除的地址id为：{}", id);
        articleService.deleteArticle(id);
        return Result.success();
    }*/


    /*
 负责人：王耀鹏
 功能：接收前端数据,响应前端,分页查询文章
 参数：page,pageSize
 返回值：Result
  */
    @GetMapping("/page")
    public Result searchpage(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询,参数:{},{}", page, pageSize);
        PageBean pageBean = articleService.page(page, pageSize);
        return Result.success(pageBean);
    }


       /*
    负责人：王耀鹏
    功能：根据id查询文章
    参数：id
    返回值：Result
     */
    @GetMapping("/{id}")
    public Result searchById(@PathVariable Integer id) {
        log.info("根据id:{}查询文章内容", id);
        Draft draft = articleService.searchById(id);
        return Result.success(draft);
    }

    @GetMapping("/content")
    public Result searchByContent(String content, @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("根据文章内容:{}查询", content);
        PageBean pageBean = articleService.searchByContent(content, page, pageSize);
        return Result.success(pageBean);
    }

       /*
    负责人：王耀鹏
    功能：根据作者姓名查找文章
    参数：authorName,page,pageSize
    返回值：Result

     */
    @GetMapping("/anthor")
    public Result searchByAnthor(String authorName, @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("根据作者:{}查询", authorName);
        PageBean pageBean = articleService.searchByAnthor(authorName, page, pageSize);
        return Result.success(pageBean);
    }



    @ApiOperation("删除文章接口")

    /*
    负责人：王楚涵
    功能：根据文章id删除文章,并且删除文章同时，对应删除文章的评论，博客和文章的关系表中对应的信息
    参数：id
    返回值：
    */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除的文章id为：{}",id);
        articleService.deleteArticle(id);
        return Result.success();
    }

    /*
   负责人：王楚涵
   功能：根据文章id新增文章
   参数：article
   返回值：
   */
    @PostMapping
    public Result addByArticle(@RequestBody Article article){
        log.info("新增的文章：{}",article);
        articleService.addArticle(article);
        return Result.success();
    }

    /*
   负责人：王楚涵
   功能：根据文章id修改文章，id存储在body里面
   参数：article
   返回值：
   */
    @PutMapping
    public Result modifyById(@RequestBody Article article){
        log.info("修改的文章为：{}",article);
        articleService.modifyArticle(article);
        return Result.success();
    }

}
